package it.evento.utils.genpage;

import it.evento.Eventsfeedback;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Ilo
 */
public class Useful {
        
    /**
     * Calculates the stars based on the passed feedback list
     * 
     * @param lst the list of the events feedback
     * @return a long representing the rounded stars
     */
    public static long calculateStars(List<Eventsfeedback> lst) {
        List<Double> ls = new LinkedList<>();
        for(Eventsfeedback ef : lst) {
            ls.add(ef.getStars());
        }
        
        Double ret = 0.0;
        
        if(ls.isEmpty())
            return 0;
        
        for(Double d : ls) {
            ret += d;
        }
        ret /= ls.size();
        
        return Math.round(ret);
    }
    
    /**
     * Cuts the text based on the first stop-word after 30 words
     * 
     * @param text the text to elaborate
     * @return the new text
     */
    public static String cutText(String text) {
        return cutText(text, 30);
    }
    /**
     * Cuts the text based on the first stop-word after the count
     * 
     * @param text the text to elaborate
     * @param maxWords the max number of words
     * @return the new text
     */
    public static String cutText(String text, int maxWords) {
        String[] words = text.split(" ");
        String ret = "";
        if(words.length > maxWords) {
            int k;
            for(k=0; k<maxWords; k++) {
                ret += words[k] + " ";
            }

            if(!words[k].endsWith(".") && !words[k].endsWith("!") && !words[k].endsWith("?")) {
                k++;
                while(k<words.length && !words[k].endsWith(".") && !words[k].endsWith("!") && !words[k].endsWith("?")) {
                    ret += words[k] + " ";
                    k++;
                }
                if(k<words.length)
                    ret += words[k];
            }

            return ret.trim();
        }
        
        return text;
    }
}
